package com.bmadmin.controller.cus;

import com.bmadmin.domain.DataQueryListPOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.service.DataQueryManagementService;
import com.bmadmin.util.BMUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 金玮良 on 2017-12-19.
 */
@RestController
@RequestMapping("/dataQueryManagement")
public class DataQueryManagementController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());

    @Autowired
    DataQueryManagementService dataQueryManagementService;

    /**
     * 获取查询清单 tree
     * @param dataQueryListPOJO
     * @return
     */

    @GetMapping("/getTreeList")
    public NormalResult getQueryList(DataQueryListPOJO dataQueryListPOJO){
        NormalResult result = dataQueryManagementService.getDataQueryByTree(dataQueryListPOJO);
        logger.info(result);
        return result;
    }

    /**
     * 新增查询清单
     * @param dataQueryListPOJO
     * @return
     */
    @PostMapping("/insert")
    public NormalResult insertQueryList(DataQueryListPOJO dataQueryListPOJO){
        NormalResult result = new NormalResult();

        if(BMUtil.isNullOrEmpty(dataQueryListPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }

//        if("".equals(BMUtil.NulltoString(dataQueryListPOJO.getL_id()))){
//            result.setCode("9999");
//            result.setMsg("传入查询清单id为空");
//            return result;
//        }
        if("".equals(BMUtil.NulltoString(dataQueryListPOJO.getL_name()))){
            result.setCode("9999");
            result.setMsg("传入查询清单名称为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(dataQueryListPOJO.getL_part_id()))){
            result.setCode("9999");
            result.setMsg("传入查询清单上级部门id为空");
            return result;
        }
        int id = dataQueryManagementService.getNextLid(dataQueryListPOJO.getL_part_id());
        String l_id = "";
        if(id<10||id>100){
        	l_id = "0"+String.valueOf(id);
        }else{
        	l_id = String.valueOf(id);
        }
        if(!"00".equals(dataQueryListPOJO.getL_part_id())&&id<100){
        	l_id = dataQueryListPOJO.getL_part_id()+l_id;
        }
        dataQueryListPOJO.setL_id(l_id);
        result=dataQueryManagementService.insertDataQueryList(dataQueryListPOJO);

        return result;
    }


    /**
     * 删除查询清单
     */
    @PostMapping("/delete")
    public NormalResult delteQueryList(DataQueryListPOJO dataQueryListPOJO){
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(dataQueryListPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(dataQueryListPOJO.getL_id()))){
            result.setCode("9999");
            result.setMsg("传入查询清单id为空");
            return result;
        }

        result = dataQueryManagementService.delDataQueryList(dataQueryListPOJO);
        return result;

    }

    /**
     * 修改查询清单
     */
    @PostMapping("/update")
    public NormalResult updateQueryList(DataQueryListPOJO dataQueryListPOJO){
        NormalResult result = new NormalResult();

        if(BMUtil.isNullOrEmpty(dataQueryListPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(dataQueryListPOJO.getL_id()))){
            result.setCode("9999");
            result.setMsg("传入查询清单id为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(dataQueryListPOJO.getL_name()))){
            result.setCode("9999");
            result.setMsg("传入查询清单名称为空");
            return result;
        }

        result = dataQueryManagementService.updateDataQueryList(dataQueryListPOJO);

        return result;
    }


}
